package crl.lottery.model
{
	import crl.lottery.model.vo.ItemVO;

	public class Logic
	{
		private var randomList:Array;
		
		public var total:int=10000;
		public var du:int;
		public function Logic()
		{
			du=360/8;
			
			randomList=new Array();
		}
		
		private var _data:Vector.<ItemVO>;
		public function set data(value:Vector.<ItemVO>):void{
			_data=value;
		}
		
		/**
		 * 创建随机数数组; 
		 * 
		 */		
		private function createRandomList():void{
			randomList.splice(0);
			
			//0度为1等奖;
			//du 为二等奖;
			//其它为;
			
			var t_pointer:int=0;
			
			var i:int;
			for each(var item:ItemVO in _data){
				for(i=0;i<item.odds;i++){
					randomList.push(item.order);
					t_pointer++;
				}
			}	
			
			
			
			//个数不足时,用递增式算法，生成一堆获奖数据;
			var yushu:int=(total-t_pointer)/5;
			var j:int;
			var k:int;
			for(i=3;i<8;i++){
				j=yushu-yushu*(5-i)/3;
				//trace(j+"个"+i)
				for(k=0;k<j;k++){
					randomList.push(i);
					t_pointer++;
				}
			}
			
			//trace(randomList.length);
			
			//填充到10000个数据;
			var temp:int;
			for(i=t_pointer;i<total;i++){
				temp=Math.ceil(Math.random()*5)+2;
				randomList.push(temp);
			}
			
			
			//随机排序一次;
			var rnd:Number;
			for(i=0;i<total;i++){
				j=total-1-i;
				rnd = Math.ceil(Math.random()*j);

				temp=randomList[rnd];
				randomList[rnd]=randomList[j];
				randomList[j]=temp;
			}
			
			//递归打乱一次;
			randomList.sort(randomsort);
			
			//trace(randomList);
		}
		
		private function randomsort(a:int, b:int):int {
			return Math.random()>Math.random() ? -1 : 1;
		}
		
		public function getNext():int{
			if(randomList.length==0){
				createRandomList();
			}
			
			return randomList.pop();
		}
	}
}